package kr.ac.inha.vota;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
    	
        Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        View vw = new MyView(this);
        addContentView(vw, new LinearLayout.LayoutParams(display.getWidth(), display.getHeight())); 
    }
    
    protected class MyView extends View {
    	public MyView(Context context) {
    		super(context);
    	}
    	
    	public int TouchRect(Rect a, Rect b, Rect c, Rect d, MotionEvent event)
    	{
    		if(a.contains((int)event.getX(),(int)event.getY()))
    			return 1;
    		else if(b.contains((int)event.getX(),(int)event.getY()))
    			return 2;
    		else if(c.contains((int)event.getX(),(int)event.getY()))
    			return 3;
    		else if(d.contains((int)event.getX(),(int)event.getY()))
    			return 4;
    		else
    			return -1;
    	}
    	
    	public boolean onTouchEvent(MotionEvent event) {
    		Intent intent;
    		
    		super.onTouchEvent(event);
    		
    		Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
            
    		Rect a = new Rect();
    		a.set((int)(0.37*display.getWidth()),(int)(0.145*display.getHeight()),
    				(int)(0.63*display.getWidth()),(int)(0.3*display.getHeight()));
    		Rect b = new Rect();
    		b.set((int)(0.37*display.getWidth()),(int)(0.358*display.getHeight()),
    				(int)(0.63*display.getWidth()),(int)(0.51*display.getHeight()));
    		Rect c = new Rect();
    		c.set((int)(0.37*display.getWidth()),(int)(0.573*display.getHeight()),
    				(int)(0.63*display.getWidth()),(int)(0.726*display.getHeight()));
    		Rect d = new Rect();
    		d.set((int)(0.37*display.getWidth()),(int)(0.788*display.getHeight()),
    				(int)(0.63*display.getWidth()),(int)(0.943*display.getHeight()));
    		
    		if(event.getAction() == MotionEvent.ACTION_DOWN) {
    			
    		switch(TouchRect(a,b,c,d,event))
    		{
    		case 1:
    			Toast.makeText(MainActivity.this, "Go to Virtual OPIc", Toast.LENGTH_SHORT).show();
    			
    			intent = new Intent(MainActivity.this, OpicMainActivity.class);
    			intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        		startActivity(intent);
        		finish();
    			break;
    		case 2:
    			Toast.makeText(MainActivity.this, "Go to Virtual Telephone", Toast.LENGTH_SHORT).show();
    			
    			intent = new Intent(MainActivity.this, TeleMainActivity.class);
    			intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        		startActivity(intent);
        		finish();
    			break;
    		case 3:
    			Toast.makeText(MainActivity.this, "Go to VoiceMemos", Toast.LENGTH_SHORT).show();
    			
    			intent = new Intent(MainActivity.this, VoicememosActivity.class);
    			intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        		startActivity(intent);
        		finish();
    			break;
    		case 4:
    			Toast.makeText(MainActivity.this, "Go to Setting", Toast.LENGTH_SHORT).show();
    			
    			intent = new Intent(MainActivity.this, SettingActivity.class);
    			intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        		startActivity(intent);
        		finish();
    			break;
    		}   			
    			return true;
    		}
    		return false;
    	}
    }
}